package starter.kit.rx.app.views.html5;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.webkit.JavascriptInterface;

/**
 * ============================================================
 * 版权： xx版权所有（c）2016
 * <p>
 * 作者：Loofer
 * 版本：1.0
 * 创建日期 ：2016/10/27 13:18.
 * 描述：
 * <p>
 * 注:如果您修改了本类请填写以下内容作为记录，如非本人操作劳烦通知，谢谢！！！
 * Modified Date Modify Content:
 * <p>
 * ==========================================================
 */
public class HTML5WebViewActivity extends AppCompatActivity {

    private HTML5CustomWebView mWebView;
    private String url;
    public static String EXTRA_URL;
    //http://www.zttmall.com/Wapshop/Topic.aspx?TopicId=18
//    private String ad_url = "http://www.zttmall.com/Wapshop/Topic.aspx?TopicId=18";
//    private String title="百度一下你就知道";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent=getIntent();
        if(intent!=null){
            url=  intent.getStringExtra(EXTRA_URL);
        }
        mWebView = new HTML5CustomWebView(this, this,"",url);
//        mWebView.setDownloadListener((url, userAgent, contentDisposition, mimetype, contentLength) -> {
//            Uri uri = Uri.parse(url);
//            Intent intent2 = new Intent(Intent.ACTION_VIEW, uri);
//            intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            startActivity(intent);
//        });
        //准备javascript注入
        mWebView.addJavascriptInterface(
                new Js2JavaInterface(),"Js2JavaInterface");
        if (savedInstanceState != null) {
            mWebView.restoreState(savedInstanceState);
        } else {
//            if (ad_url != null) {
//                mWebView.loadUrl(ad_url);
//            }
        }
        mWebView.loadUrl(url);
        setContentView(mWebView.getLayout());
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (mWebView != null) {
            mWebView.saveState(outState);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mWebView != null) {
            mWebView.onResume();
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        if (mWebView != null) {
            mWebView.stopLoading();
        }
    }
    @Override
    protected void onPause() {
        super.onPause();
        if (mWebView != null) {
            mWebView.onPause();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mWebView != null) {
            mWebView.doDestroy();
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return super.onTouchEvent(event);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return super.onKeyDown(keyCode, event);
    }
    @Override
    public void onBackPressed() {
        if (mWebView != null) {
            if(mWebView.canGoBack()){
                mWebView.goBack();
            }else{
                mWebView.releaseCustomview();
            }
        }
        super.onBackPressed();
    }
    /**
     * JavaScript注入回调
     */
    public class Js2JavaInterface {
        private Context context;
        private String TAG = "Js2JavaInterface";
        @JavascriptInterface
        public void showProduct(String productId){
            if(productId!=null){
                //进行跳转商品详情
//                showToastMsgShort("点击的商品的ID为:" + productId);
            }else {
//                showToastMsgShort("商品ID为空!");
            }
        }
    }


}
